package com.agileai.bpm.common;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.bpm.model.ActivitiesDocument.Activities;
import com.agileai.bpm.model.EndActivityType;
import com.agileai.bpm.model.ManualActivityType;
import com.agileai.bpm.model.ProcessDefineDocument.ProcessDefine;
import com.agileai.bpm.model.StartActivityType;
import com.agileai.bpm.model.TransitionType;
import com.agileai.bpm.model.TransitionsDocument.Transitions;
import com.agileai.domain.DataRow;

public class GraphBuilder {
	
	public GraphBuilder(){
		
	}
	
	public String createDefaultJsonData(ProcessDefine processDefine){
		String result = null;
		try {
			JSONObject root = new JSONObject();
			JSONObject states = new JSONObject();
			JSONObject paths = new JSONObject();
			JSONObject props = new JSONObject();
			root.put("states", states);
			root.put("paths", paths);
			root.put("props", props);
			
			this.initStates(states,processDefine);
			this.initPaths(paths,processDefine);
			this.initProps(props,processDefine);
			result = root.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	private void initStates(JSONObject states,ProcessDefine processDefine){
		try {
			Activities activities = processDefine.getActivities();
			StartActivityType startActivityType = activities.getStartActivity();
			
			JSONObject rec1 = new JSONObject();
			rec1.put("type", "start");
			rec1.put("text", new DataRow("text",startActivityType.getName()));
			rec1.put("attr", new DataRow("x",406,"y",43,"width",100,"height",50));
			
			JSONObject rec1Props = new JSONObject();
			rec1Props.put("text", new DataRow("value",startActivityType.getName()));
			rec1Props.put("key", new DataRow("value",startActivityType.getId()));
			rec1Props.put("code", new DataRow("value",startActivityType.getCode()));
			rec1.put("props", rec1Props);
			
			states.put(startActivityType.getCode(), rec1);
			
			ManualActivityType manualActivityType = activities.getManualActivityArray(0);
			JSONObject rec2 = new JSONObject();
			rec2.put("type", "manual");
			rec2.put("text", new DataRow("text",manualActivityType.getName()));
			rec2.put("attr", new DataRow("x",406,"y",165,"width",100,"height",50));
			
			JSONObject rec2Props = new JSONObject();
			rec2Props.put("text", new DataRow("value",manualActivityType.getName()));
			rec2Props.put("key", new DataRow("value",manualActivityType.getId()));
			rec2Props.put("code", new DataRow("value",manualActivityType.getCode()));
			rec2.put("props", rec2Props);
			
			states.put(manualActivityType.getCode(), rec2);
			
			
			EndActivityType endActivityType = activities.getEndActivity();
			JSONObject rec3 = new JSONObject();
			rec3.put("type", "end");
			rec3.put("text", new DataRow("text",endActivityType.getName()));
			rec3.put("attr", new DataRow("x",406,"y",286,"width",100,"height",50));
			
			JSONObject rec3Props = new JSONObject();
			rec3Props.put("text", new DataRow("value","结束"));
			rec3Props.put("key", new DataRow("value",endActivityType.getId()));
			rec3Props.put("code", new DataRow("value",endActivityType.getCode()));
			rec3.put("props", rec3Props);
			
			states.put(endActivityType.getCode(), rec3);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void initPaths(JSONObject paths,ProcessDefine processDefine){
		try {
			Transitions transitions = processDefine.getTransitions();
			TransitionType transitionType1 = transitions.getTransitionArray(0);
			
			JSONObject path4 = new JSONObject();
			path4.put("from", "rect1");
			path4.put("to", "rect2");
			path4.put("dots", new JSONArray());
			path4.put("text", new JSONObject(new DataRow("text",transitionType1.getName())));
			path4.put("textPos", new JSONObject(new DataRow("x",43,"y",2)));
			JSONObject path4PropsJsonObject = new JSONObject();
			path4PropsJsonObject.put("text", new DataRow("value",""));
			path4PropsJsonObject.put("key", new DataRow("value",transitionType1.getId()));
			path4PropsJsonObject.put("code", new DataRow("value",transitionType1.getCode()));
			path4.put("props", path4PropsJsonObject);
			paths.put(transitionType1.getCode(), path4);
			
			TransitionType transitionType2 = transitions.getTransitionArray(1);
			JSONObject path5 = new JSONObject();
			path5.put("from", "rect2");
			path5.put("to", "rect3");
			path5.put("dots", new JSONArray());
			path5.put("text", new JSONObject(new DataRow("text",transitionType2.getName())));
			path5.put("textPos", new JSONObject(new DataRow("x",35,"y",3)));
			JSONObject path5PropsJsonObject = new JSONObject();
			path5PropsJsonObject.put("text", new DataRow("value",""));
			path5PropsJsonObject.put("key", new DataRow("value",transitionType2.getId()));
			path5PropsJsonObject.put("code", new DataRow("value",transitionType2.getCode()));
			path5.put("props", path5PropsJsonObject);
			paths.put(transitionType2.getCode(), path5);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void initProps(JSONObject props,ProcessDefine processDefine){
		try {
			String processId = processDefine.getId();
			
			JSONObject jsonObject = new JSONObject();
			props.put("props", jsonObject);
			
			jsonObject.put("name", new JSONObject(new DataRow("value",processDefine.getName())));
			jsonObject.put("key", new JSONObject(new DataRow("value",processId)));
			jsonObject.put("index", new JSONObject(new DataRow("value",5)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
